package com.leff.cards.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gcm.GCMRegistrar;
import com.leff.cards.R;
import com.leff.cards.data.AppState;
import com.leff.cards.util.Secrets;
import com.leff.cards.util.Settings;
import com.leff.cards.web.QueryDispatcher;
import com.leff.cards.web.requests.GcmRegisterRequest;
import com.leff.cards.web.requests.GcmUnregisterRequest;

public class SettingsFragment extends SherlockFragment implements Secrets, OnCheckedChangeListener {
	
	private boolean mNotifSettingAtStart;
	private CheckBox mNotifCheckBox;
	private CheckBox mFacebookCheckBox;
	
	private RadioGroup mVoteGroup;	
	private RadioGroup mInviteGroup;
	
	private LinearLayout mPostOptionsFrame;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(container == null) {
			return null;
		}
		View V = inflater.inflate(R.layout.settings, null);
		
		TextView accountText = (TextView)V.findViewById(R.id.account_text);
		accountText.setText(AppState.get().getUser().fbid + " / " + AppState.get().getUser().id);
		
		mNotifCheckBox = (CheckBox)V.findViewById(R.id.notifications_checkbox);
		mFacebookCheckBox = (CheckBox)V.findViewById(R.id.facebook_checkbox);
		
		mFacebookCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton button, boolean checked) {
				Settings.setPostToFacebook(checked);
				refreshFacebookOptions();
			}
		});
		
		mVoteGroup = (RadioGroup)V.findViewById(R.id.post_votes_group);
		mVoteGroup.setOnCheckedChangeListener(this);
		
		mInviteGroup = (RadioGroup)V.findViewById(R.id.post_invites_group);
		mInviteGroup.setOnCheckedChangeListener(this);
		
		mPostOptionsFrame = (LinearLayout)V.findViewById(R.id.post_options_frame);
		refreshFacebookOptions();
		
		return V;
	}

	@Override
	public void onStart() {
		super.onStart();
		mNotifSettingAtStart = Settings.getNotificationsEnabled();
		mNotifCheckBox.setChecked(mNotifSettingAtStart);
		
		mFacebookCheckBox.setChecked(Settings.getPostToFacebook());
		refreshFacebookOptions();
	}

	@Override
	public void onStop() {
		super.onStop();

		String regId = GCMRegistrar.getRegistrationId(getActivity());
		if(regId == null || regId.equals("")) {
			Log.v(TAG, "Can't proceed with (un)register - no registration id");
			return;
		}
		
		long userId = AppState.get().getUser().id;
		
		boolean checked = mNotifCheckBox.isChecked();		
		if(checked && !mNotifSettingAtStart) {
			new QueryDispatcher(new GcmRegisterRequest(userId, regId)).execute();
		}
		else if(!checked && mNotifSettingAtStart) {
			new QueryDispatcher(new GcmUnregisterRequest(userId, regId)).execute();
		}
	}
	
	private void refreshFacebookOptions() {
		
		boolean on = Settings.getPostToFacebook();
		
		mFacebookCheckBox.setChecked(on);
		mPostOptionsFrame.setVisibility(on ? View.VISIBLE : View.GONE);
		
		int choice = Settings.getVotePostPreference();
		switch(choice) {
			case Settings.FB_POST_NEVER:
				mVoteGroup.check(R.id.post_votes_never);
				break;
			case Settings.FB_POST_PROMPT:
				mVoteGroup.check(R.id.post_votes_prompt);
				break;
			case Settings.FB_POST_ALWAYS:
				mVoteGroup.check(R.id.post_votes_always);
				break;
		}
		
		choice = Settings.getInvitePostPreference();
		switch(choice) {
			case Settings.FB_POST_NEVER:
				mInviteGroup.check(R.id.post_invites_never);
				break;
			case Settings.FB_POST_PROMPT:
				mInviteGroup.check(R.id.post_invites_prompt);
				break;
			case Settings.FB_POST_ALWAYS:
				mInviteGroup.check(R.id.post_invites_always);
				break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int buttonId) {
		
		switch(buttonId) {
			case R.id.post_votes_never:
				Settings.setVotePostPreference(Settings.FB_POST_NEVER);
				break;
			case R.id.post_votes_prompt:
				Settings.setVotePostPreference(Settings.FB_POST_PROMPT);
				break;
			case R.id.post_votes_always:
				Settings.setVotePostPreference(Settings.FB_POST_ALWAYS);
				break;

			case R.id.post_invites_never:
				Settings.setInvitePostPreference(Settings.FB_POST_NEVER);
				break;
			case R.id.post_invites_prompt:
				Settings.setInvitePostPreference(Settings.FB_POST_PROMPT);
				break;
			case R.id.post_invites_always:
				Settings.setInvitePostPreference(Settings.FB_POST_ALWAYS);
				break;
		}
	}
}
